import { storage as _storage, tokenTableName } from '@/config'

const storage = _storage || 'localStorage'

/**
 * @description 获取token
 */
export const getToken = () => {
  if ('localStorage' === storage)
    return uni.getStorage({
      key: tokenTableName,
    })
  else if ('sessionStorage' === storage) return uni.getStorageSync(tokenTableName)
}

/**
 * @description 存储token
 * @param token
 */
export const setToken = (token: string) => {
  if ('localStorage' === storage)
    return uni.setStorage({
      key: tokenTableName,
      data: token,
    })
  else if ('sessionStorage' === storage) return uni.setStorageSync(tokenTableName, token)
}

/**
 * @description 移除token
 */
export const removeToken = (storage = 'localStorage') => {
  if ('localStorage' === storage)
    return uni.removeStorage({
      key: tokenTableName,
    })
  else if ('sessionStorage' === storage) return uni.removeStorageSync(tokenTableName)
}

/**
 * @description 清除缓存
 */

export const clearStorage = () => {
  if ('localStorage' === storage) uni.clearStorage()
  else if ('sessionStorage' === storage) uni.clearStorageSync()
}
